package com.learner.controller;

import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learner.entity.Department;
import com.learner.entity.EquipmentType;
import com.learner.entity.User;
import com.learner.service.DepartmentService;
import com.learner.service.UserService;
import com.learner.utils.PageUtil;
import com.learner.utils.ResponseUtil;
import com.learner.utils.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: qho
 * @Date: 2018/10/12 00:22
 * @Description:
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private DepartmentService departmentService;

    @RequestMapping("/login")
    public String login(User user, HttpServletRequest request)throws Exception{
        User resultUser=userService.login(user);
        if(resultUser==null){
            request.setAttribute("user", user);
            request.setAttribute("errorMsg", "用户名或密码错误！");
            return "login";
        }else{
            HttpSession session=request.getSession();
            session.setAttribute("currentUser", resultUser);
            return "redirect:/main.jsp";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/login.jsp";
    }

    @RequestMapping("/list")
    public ModelAndView list(@RequestParam(value="page",required=false,defaultValue = "1")Integer page, User s_user, HttpServletRequest request){
        ModelAndView mav=new ModelAndView();
        HttpSession session=request.getSession();
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("userName", StringUtil.formatLike(s_user.getUserName()));
        PageHelper.startPage(page, 5);
        List<User> userList=userService.find(map);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        String pageCode= PageUtil.getPagation(request.getContextPath()+"/user/list.do", Convert.toInt(pageInfo.getTotal()), page, 5);
        mav.addObject("pageCode", pageCode);
        mav.addObject("modeName", "用户管理");
        mav.addObject("userList", userList);
        mav.addObject("mainPage", "user/list.jsp");
        mav.setViewName("main");
        return mav;
    }

    @RequestMapping("/delete")
    public void delete(@RequestParam(value="id")String id, HttpServletResponse response)throws Exception{
        JSONObject result=new JSONObject();
        userService.delete(Integer.parseInt(id));
        result.put("success", true);
        ResponseUtil.write(result, response);
    }

    @RequestMapping("/preSave")
    public ModelAndView preSave(@RequestParam(value="id",required=false)String id){
        ModelAndView mav=new ModelAndView();
        mav.addObject("mainPage", "user/save.jsp");
        mav.addObject("modeName", "用户管理");
        mav.setViewName("main");
        if(StringUtil.isNotEmpty(id)){
            mav.addObject("actionName", "用户修改");
            User user=userService.loadById(Integer.parseInt(id));
            mav.addObject("user", user);
        }else{
            mav.addObject("actionName", "用户添加");
        }
        List<Department> departmentList=departmentService.find(null);
        mav.addObject("departmentList",departmentList);
        return mav;
    }

    @RequestMapping("/save")
    public String save(User user){
        if(user.getId()==null){
            userService.add(user);
        }else{
            userService.update(user);
        }
        return "redirect:/user/list.do";
    }
}
